<template>
  <div class="add">
    <el-form :model="ruleForm"
             :rules="rules"
             ref="ruleForm"
             label-width="100px"
             class="yuyi-ruleForm">
      <el-form-item label="分类名" prop="categoryName">
        <el-input size="small" clearable v-model="ruleForm.categoryName"></el-input>
      </el-form-item>
      <el-form-item label="分类编码" prop="classificationCode">
        <el-input size="small" clearable v-model="ruleForm.classificationCode"></el-input>
      </el-form-item>
      <el-form-item label="分类序号" prop="classificationNumber">
        <el-input size="small" clearable v-model="ruleForm.classificationNumber"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"
                   size="small"
                   :loading="saveLoading"
                   @click="submitForm('ruleForm')">
          {{type === 'add' ? '保存' : '修改'}}
        </el-button>
        <el-button size="small" @click="handleCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'addClassification', // 新增实体分类号
  props: {
    saveLoading: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      ruleForm: {
        categoryName: '',
        classificationCode: '',
        classificationNumber: ''
      },
      rules: {
        categoryName: [
          {
            required: true,
            message: '请输入活动名称',
            trigger: 'blur'
          }
        ],
        classificationCode: [
          {
            required: true,
            message: '请输入分类编码',
            trigger: 'blur'
          }
        ],
        classificationNumber: [
          {
            required: true,
            message: '请输入分类序号',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  watch: {
    rowData: {
      handler (val) {
        this.ruleForm = val
      },
      deep: true
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('handleSubmit', this.ruleForm)
        } else {
          return false
        }
      })
    },
    handleCancel () {
      this.$emit('handleCancel')
    }
  }
}
</script>

<style lang="scss" scoped>
  .add {
    padding-right: 40px;
  }
</style>
